//
//  GQHPhoneController.m
//  Xanthium
//
//  Created by GuanQinghao on 24/02/2018.
//  Copyright © 2018 GuanQinghao. All rights reserved.
//

#pragma mark Macro
#import "GQHHeader.h"

#pragma mark Model
#import "GQHUserModel.h"

#pragma mark Object

#pragma mark View
#import "GQHPhoneView.h"

#pragma mark Controller
#import "GQHPhoneController.h"
#import "GQHPasswordSettingController.h"

#pragma mark CocoaPods

#pragma mark ----------Header End----------

@interface GQHPhoneController () <GQHPhoneViewDelegate> {
    
#pragma mark Data
    GQHUserModel *userModel;
#pragma mark View
    
}

@end

@implementation GQHPhoneController

#pragma mark --Lifecycle
- (void)viewDidLoad {
    [super viewDidLoad];
    DLog();
    
    // 快速添加视图背景图
    self.view.layer.contents = (id)[UIImage imageNamed:@"login_background"].CGImage;
    
    [self loadUserInterface];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    DLog();
    
    self.qh_titleLabel.text = @"验证手机号";
    self.qh_navigationBar.backgroundColor = kClearColor;
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    DLog();
}

#pragma mark --Delegate
/// 请求验证码
- (void)qh_requestCodeWithMobile:(NSString *)mobile {
    
    [GQHUserModel qh_requestCodeWithMobile:mobile success:^(BOOL status, NSNumber *code, NSString *message, GQHUserModel *user) {
        
        if (0 == [code intValue]) {
            
            userModel = user;
        }
    } failure:^(NSError *error) {
        
        NSString *errorString = [NSString stringWithFormat:@"网络出错:%@,code:%ld", error.domain, (long)error.code];
        [self qh_alertWithTitle:nil message:errorString actionTitle:nil handler:nil completion:nil];
    }];
}

/// 验证手机号和验证码是否正确
- (void)qh_confirmMobile:(NSString *)mobile code:(NSString *)codeString {
    
    if ([mobile isEqualToString:userModel.qh_mobile] && [codeString isEqualToString:userModel.qh_mobileCode]) {
        
        GQHPasswordSettingController *passwordSettingController = [[GQHPasswordSettingController alloc] init];
        passwordSettingController.qh_mobile = mobile;
        passwordSettingController.qh_isRegistered = self.qh_isRegistered;
        [self.navigationController pushViewController:passwordSettingController animated:YES];
    } else {
        
        [self qh_alertWithTitle:nil message:@"您输入的手机号验证码不匹配,请重新输入！" actionTitle:nil
                        handler:nil completion:nil];
    }
}

#pragma mark --TargetMethod
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    
    // 全局统一收起键盘
    [[[UIApplication sharedApplication] keyWindow] endEditing:YES];
}
#pragma mark --PrivateMethod

#pragma mark --Setter

#pragma mark --Getter

#pragma mark --View
- (void)loadUserInterface {
    
    CGRect frame = CGRectMake(0, self.qh_navigationBar.qh_bottom, self.view.qh_contentAreaWidth, self.view.qh_safeAreaHeight - self.view.qh_navigationBarHeight + self.view.qh_homeIndicatorHeight);
    GQHPhoneView *phoneView = [[GQHPhoneView alloc] initWithFrame:frame];
    phoneView.qh_delegate = self;
    [self.view addSubview:phoneView];
}

@end
